<?php

class oh_timeslot
{
	public $time;
	public $slot_id;
	public $duration;
}


function oh_slot_add($year,$month,$day,$hour,$minute,$duration)
{
	$con=oh_sql_connect();
	
//	echo $year."-".$month."-".$day." ".$hour.":".$minute.":00<br />" ;

	// every value here should be an integer
	$year = filter_var($year, FILTER_SANITIZE_NUMBER_INT);
	$month = filter_var($month, FILTER_SANITIZE_NUMBER_INT);
	$day = filter_var($day, FILTER_SANITIZE_NUMBER_INT);
	$hour = filter_var($hour, FILTER_SANITIZE_NUMBER_INT);
	$minute = filter_var($minute, FILTER_SANITIZE_NUMBER_INT);
	$duration = filter_var($duration, FILTER_SANITIZE_NUMBER_INT);
	
//	echo $year."-".$month."-".$day." ".$hour.":".$minute.":00";
	
	if ($year < 0 || $hour < 0 || $hour > 23 || $month < 1 || $month > 12 || $day < 1 || $day > 31 || $minute < 0 || $minute >59) {
		return "invalid";
	}
	else 
	{
		$timestring = $year."-".$month."-".$day." ".$hour.":".$minute.":00";
	
		//just in case
		$timestring=$con->real_escape_string($timestring);
	
		$sql = "INSERT INTO `oh_timeslots` (`slot_time`,`slot_len`) VALUES ('".$timestring."','".$duration."')";
		
		oh_sql_query($con,$sql);
	}
	return "success";
}

function oh_slot_update($slot_id,$year,$month,$day,$hour,$minute,$duration)
{
	$con=oh_sql_connect();
	
//	echo $year."-".$month."-".$day." ".$hour.":".$minute.":00<br />" ;

	// every value here should be an integer
	$slot_id = filter_var($slot_id, FILTER_SANITIZE_NUMBER_INT);
	$year = filter_var($year, FILTER_SANITIZE_NUMBER_INT);
	$month = filter_var($month, FILTER_SANITIZE_NUMBER_INT);
	$day = filter_var($day, FILTER_SANITIZE_NUMBER_INT);
	$hour = filter_var($hour, FILTER_SANITIZE_NUMBER_INT);
	$minute = filter_var($minute, FILTER_SANITIZE_NUMBER_INT);
	$duration = filter_var($duration, FILTER_SANITIZE_NUMBER_INT);
	
//	echo $year."-".$month."-".$day." ".$hour.":".$minute.":00";
	
	if ($year < 0 || $hour < 0 || $hour > 23 || $month < 1 || $month > 12 || $day < 1 || $day > 31 || $minute < 0 || $minute >59) {
		return "invalid";
	}
	else 
	{
		$timestring = $year."-".$month."-".$day." ".$hour.":".$minute.":00";
	
		//just in case
		$timestring=$con->real_escape_string($timestring);
		
		$sql="UPDATE `oh_timeslots` set 
			`slot_time` = '".$timestring."',
			`slot_len` = '".$duration."' WHERE
			`slot_id`='".$slot_id."'";
		
		oh_sql_query($con,$sql);
	}
	return "success";
}

function oh_slot_delete($slot_id)
{
	$con=oh_sql_connect();
	
	$slot_id = filter_var($slot_id, FILTER_SANITIZE_NUMBER_INT);
	
	$sql="DELETE from `oh_timeslots` WHERE `slot_id`='".$slot_id."'";
	
	oh_sql_query($con,$sql);

}

function oh_render_slot_table()
{
	$con=oh_sql_connect();
	
	$sql="SELECT * from `oh_timeslots` ORDER BY `slot_time` ASC";
	
	$result=oh_sql_query($con,$sql);
	
	if( $result->num_rows > 0)
	{	
		echo "<table>\n";
		while($row=$result->fetch_assoc())
		{
			echo "<tr>\n";
			echo "<td class=\"scentabledata\">Slot #".$row['slot_id']."</td>\n";
			echo "<td class=\"scentabledata\">".$row['slot_time']."</td>\n";
			echo "<td class=\"scentabledata\">".$row['slot_len']." hours</td>\n";	
			echo "<td class=\"scentabledata\">";
			make_button("Edit","slotedit.php?code=".$row['slot_id']);
			make_button("Delete","slots.php?delete=".$row['slot_id']);
			echo "</td>";
			echo "</tr>\n";
		}
		echo "</table>\n";
	}
	else
	{
		echo "None found.";
	}
		
	return $slots;
}

function oh_get_slot_time($slot_id)
{
	$con=oh_sql_connect();
	
	$slot_id = filter_var($slot_id, FILTER_SANITIZE_NUMBER_INT);
	
	$sql="SELECT * from `oh_timeslots` WHERE `slot_id` = '".$slot_id."'";
	
	$result=oh_sql_query($con,$sql);
	
	// overload duration into seconds field for ease of data passing:
	
	if( $result->num_rows > 0 && $row=$result->fetch_assoc())
	{
	
		$time=DateTime::createFromFormat('Y-m-d H:i:s',$row['slot_time']);
		$duration=$row['slot_len'];
		$modifier="+".$duration." seconds";
		$time->modify($modifier);
	}
	
	
	return $time;
}

function oh_get_all_slots()
{
	$con=oh_sql_connect();
	
	$slot_id = filter_var($slot_id, FILTER_SANITIZE_NUMBER_INT);
	
	$sql="SELECT * from `oh_timeslots` ORDER BY `slot_time` ASC";
	
	$result=oh_sql_query($con,$sql);
	
	$slots=array();
	$i=0;
	
	// overload duration into seconds field for ease of data passing:
	
	if( $result->num_rows > 0)
	while($row=$result->fetch_assoc())
	{
		$slot=new oh_timeslot();
		$slot->time=DateTime::createFromFormat('Y-m-d H:i:s',$row['slot_time']);
		$slot->duration=$row['slot_len'];
		$slot->slot_id=$row['slot_id'];
				
		$slots[$i]=$slot;
		$i++;
		
	}
	
	$j=0;
	
	return $slots;
}

function oh_slot_print($slot)
{
	// Print day
	echo $slot->time->format("D").", ".$slot->time->format("M")." ".$slot->time->format("j")."<br/>\n";
		
	// Print start time
	// TODO: Expand to support 24 hour format if user chooses
	echo $slot->time->format("g:i").$slot->time->format("a");
	
	echo " - ";
	
	$modifier="+ ".$slot->duration." hours";
	$slot->time->modify($modifier);
	
	// Now print the close time
	echo $slot->time->format("g:i").$slot->time->format("a");
}

?>
